package com.bmob.im.demo.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bmob.im.demo.BuildConfig;
import com.bmob.im.demo.R;
import com.bmob.im.demo.sns.TencentShareConstants;
import com.umeng.fb.FeedbackAgent;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;
import com.umeng.update.UpdateStatus;


public class AboutActivity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout mCheckUpdateLayout, mShareLayout, mFeedBackLayout, mLinkLayout;
    private TextView mVersionText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        findView();
        initView();
    }

    void findView() {
        setContentView(R.layout.activity_about);
        initTopBarForLeft("关于点滴");
        mCheckUpdateLayout = (RelativeLayout) findViewById(R.id.activity_about_check_update_layout);
        mShareLayout = (RelativeLayout) findViewById(R.id.activity_about_share_layout);
        mFeedBackLayout = (RelativeLayout) findViewById(R.id.activity_about_feedback_layout);
        mLinkLayout = (RelativeLayout) findViewById(R.id.activity_about_link_layout);
        mVersionText = (TextView) findViewById(R.id.activity_about_version_text);
    }

    void initView() {
        mVersionText.setText("v" + BuildConfig.VERSION_NAME);
        bindEvent();
    }

    void bindEvent() {
        mCheckUpdateLayout.setOnClickListener(this);
        mShareLayout.setOnClickListener(this);
        mFeedBackLayout.setOnClickListener(this);
        mLinkLayout.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.activity_about_check_update_layout:
                checkUpdate();
                break;
            case R.id.activity_about_share_layout:
                shareToFriend();
                break;
            case R.id.activity_about_feedback_layout:
                FeedbackAgent agent = new FeedbackAgent(AboutActivity.this);
                agent.startFeedbackActivity();
                break;
            case R.id.activity_about_link_layout:
                startActivity(new Intent(AboutActivity.this, LinkAcitivity.class));
                break;
        }
    }

    private void checkUpdate() {
        Toast.makeText(mContext, "正在检查。。。", Toast.LENGTH_SHORT).show();
        UmengUpdateAgent.setUpdateAutoPopup(false);
        UmengUpdateAgent.setUpdateListener(new UmengUpdateListener() {
            @Override
            public void onUpdateReturned(int updateStatus, UpdateResponse updateInfo) {
                switch (updateStatus) {
                    case UpdateStatus.Yes: // has update
                        Log.e(TAG, "有更新");
                        UmengUpdateAgent.showUpdateDialog(mContext, updateInfo);
                        break;
                    case UpdateStatus.No: // has no update
                        Toast.makeText(mContext, "没有更新", Toast.LENGTH_SHORT).show();
                        break;
                    case UpdateStatus.NoneWifi: // none wifi
                        Toast.makeText(mContext, "没有wifi连接， 只在wifi下更新", Toast.LENGTH_SHORT).show();
                        break;
                    case UpdateStatus.Timeout: // time out
                        Toast.makeText(mContext, "请检查网络", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
        UmengUpdateAgent.forceUpdate(mContext);
    }

    private void shareToFriend() {
        Intent localIntent1 = new Intent("android.intent.action.SEND");
        localIntent1.setType("text/plain");
        localIntent1.putExtra("android.intent.extra.SUBJECT", "分享");
        localIntent1.putExtra("android.intent.extra.TEXT", "点滴是记录生活中重要的日子的小工具。还在为女友突然问你们相恋了多久而瞠目结舌吗？还在为关键时刻忘记女友的生日而发愁吗？那么这个小工具正是你需要的。赶快去前往" + TencentShareConstants.TARGET_URL + "下载吧！/\n");
        startActivity(Intent.createChooser(localIntent1, "分享给好友"));

    }


}
